package io.renren.project.demo.service.impl;

import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import io.renren.modules.wyzs.mobile_BillAdapterMoudle.*;
import io.renren.modules.wyzs.mobile_moudle.SalesOrderEntity;
import io.renren.modules.wyzs.service.FactoryService;
import io.renren.modules.wyzsphone.commWithWCF.BillTempGeter;
import io.renren.modules.wyzsphone.commWithWCF.CommWithWCF;
import io.renren.modules.wyzsphone.commWithWCF.XModeRtn;
import io.renren.modules.wyzsphone.commWithWCF.XModeSnd;
import io.renren.modules.wyzsphone.utils.Utils;
import io.renren.project.demo.service.DemoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.DecimalFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class DemoServiceImpl implements DemoService {

    @Autowired
    FactoryService factoryService;

    /**
     * 获得number
     */
    @Override
    public Map getNumber(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("M00");
        Value.setMethod("GetNumber");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("classtype", "Sales");
        Value.getData().setData("organizeid", (Integer) params.get("organizeId"));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        String ValueRtn = ser.Code2(Value);
        Map maps= Utils.JsonToMap(ValueRtn);
        return maps;
    }

    @Override
    public BillTemp getBillTemp(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("M00");
        Value.setMethod("GetBillTemp");
        Value.setUID((String) params.get("uid"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType("Sales");
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        BillTemp billTemp=_gson.fromJson(ValueRtn.getJsonObject(),BillTemp.class);
        return billTemp;
    }

    @Override
    public Map getAllTask(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X10");//todo
        Value.setMethod("M_GetSalesTaskCar");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("type", "AllSalesTaskInfo");
        Value.getData().setData("currentpage", params.get("currentpage".toString()));//todo



        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=JsonToList(_gson,ValueRtn);
        Map maps=new HashMap();
        maps.put("list",list);
        return maps;
    }

    @Override
    public Map getSalesLogist(Map<String, Object> params) {


        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X10");//todo
        Value.setMethod("M_GetFleet");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("organize", (String)params.get("organizeId"));//todo
        Value.getData().setData("continername", (String)params.get("continername"));//todo
        Value.getData().setData("currentpage", (String)params.get("currentpage"));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        Map maps=JsonToMap(_gson,ValueRtn);
        return maps;
    }

    @Override
    public Map getSalesItem(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X10");//todo
        Value.setMethod("M_GetSalesItem");
        Value.setUID((String) params.get("uid"));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        Map maps=JsonToMap(_gson,ValueRtn);
        return maps;
    }

    @Override
    public Map getAllOrder(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X10");//todo
        //Value.setMethod("W_GetSalesOrderList");
        Value.setMethod("M_GetSalesOrder");//M_GetSalesAccountList  M_GetSalesOrder
        Value.setUID((String) params.get("uid"));
       String itemid=(String)params.get("itemid");
       if(itemid!=null&&itemid.equals("")) {
           Value.getData().setData("itemid", itemid);
       }else {
           Value.getData().setData("itemid", "");
       }
        String itemtype=(String)params.get("itemtype");
        if(itemid!=null&&itemid.equals("")) {
            Value.getData().setData("itemtype", itemtype);
        }else {
            Value.getData().setData("itemtype", "");
        }
        Value.getData().setData("currentpage", (String)params.get("currentpage"));//todo
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        List list=JsonToList(_gson,ValueRtn);
        Map map=new HashMap();
        map.put("list",list);
        return map;
    }

    @Override
    public Map getOrderDetail(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X10");//todo
        Value.setMethod("M_GetDetailSalesOrder");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("organizeid", (String)params.get("organizeid"));//todo
        Value.getData().setData("orderid", (String)params.get("orderid"));//todo
        Value.getData().setData("assid", (String)params.get("assid"));//todo
        Value.getData().setData("itemid", (String)params.get("itemid"));//todo
        Value.getData().setData("customerid", (Integer)params.get("customerid"));//todo



        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        Map maps=JsonToMap(_gson,ValueRtn);
        return maps;
    }

    @Override
    public Map getAllSalesCarByorder(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);

        Value.setFile("X10");
        Value.setMethod("M_GetDetailSalesOrder");
        Value.setUID((String) params.get("uid"));
        Value.getData().setData("type", "SalesByOrder");
        Value.getData().setData("currentpage", (String)params.get("currentpage"));//todo
        Value.getData().setData("assid", (String)params.get("assid"));//todo
        Value.getData().setData("orderid", (String)params.get("orderid"));//todo
        Value.getData().setData("orderassid", (String)params.get("orderassid"));//todo

        Double d= (Double) params.get("organizeid");
        DecimalFormat df = new DecimalFormat("######0"); //四色五入转换成整数
        Integer i= Integer.parseInt(df.format(d));
        Value.getData().setData("organizeid", i);//todo

        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        Value.setJsonObject(_gson.toJson(geter));
        XModeRtn ValueRtn = ser.Code(Value);
        Map maps=JsonToMap(_gson,ValueRtn);
        maps.put("list",maps);
        return maps;
    }

    @Override
    public Map cancleTask(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillTempGeter billTempGeter=new BillTempGeter();
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X10");
        Value.setMethod("  ");
        Value.setUID((String) params.get("uid"));
        Double fid=Double.parseDouble((String) params.get("fid"));
        Integer f= fid.intValue();
        Value.getData().setData("fid", f);
        BillGeter billGeter=new BillGeter();
        billGeter.setID(f.toString());
        billGeter.setCalssType("Sales");
        Value.setJsonObject(_gson.toJson(billGeter));
        BillTempGeter geter = new BillTempGeter();
        geter.setClassType(billTempGeter.getClassType());
        XModeRtn ValueRtn = ser.Code(Value);
        Map maps=JsonToMap(_gson,ValueRtn);
        return maps;
    }


    @Override
    public Map addTask(Map<String, Object> params) {
        Gson _gson = new Gson();
        CommWithWCF ser = new CommWithWCF();
        XModeSnd Value = new XModeSnd();
        BillSaver billSaver=getBillSaver(params);
        billSaver.setCalssType("Sales");
        billSaver.setStatus("New");
        String url=factoryService.getUrlById((String)params.get("factoryId"));
        Value.setURL(url);
        Value.setFile("X10");
        Value.setMethod("M_SalesSave");
        Value.setUID((String) params.get("uid"));
        System.out.println(params.get("FCustomName"));
        Value.setJsonObject(_gson.toJson(billSaver));
        //Value.setJsonObject("{\"CalssType\":\"Sales\",\"ID\":0,\"Logs\":[],\"NeedSave\":true,\"NewStamp\":\"352a60cf-2dee-456-9aee-acd46ee41e9a\",\"SaveEntrys\":{\"1\":{\"Page\":1,\"Rows\":[{\"DataExecType\":\"Insert\",\"Fields\":{\"439\":{},\"272\":{\"NewValue\":\"\"},\"26\":{\"NewValue\":1.0},\"576\":{\"NewValue\":\"000.004\"},\"19\":{\"NewValue\":\"000017\"},\"112\":{\"NewValue\":\"000.004\"},\"24\":{\"NewValue\":646.0},\"17\":{\"NewValue\":\"6\"},\"21\":{\"NewValue\":\"成武金利宏新能源有限公司\"},\"213\":{\"NewValue\":1000.0},\"438\":{}},\"Index\":0,\"RowID\":0}]}},\"SaveItems\":{\"33\":{\"NewValue\":6},\"53\":{},\"1\":{\"NewValue\":\"000167\"},\"462\":{},\"62\":{\"NewValue\":\"\"},\"50\":{},\"6\":{\"NewValue\":\"01.01.002\"},\"56\":{},\"41\":{},\"200\":{},\"575\":{},\"238\":{},\"42\":{},\"60\":{\"NewValue\":0.0},\"58\":{\"NewValue\":\"成武金利宏新能源有限公司\"},\"52\":{},\"55\":{},\"46\":{},\"14\":{\"NewValue\":0},\"51\":{},\"7\":{\"NewValue\":0},\"177\":{},\"2\":{},\"8\":{\"NewValue\":1.0},\"44\":{},\"49\":{\"NewValue\":\"00006\"},\"501\":{},\"30\":{\"NewValue\":0},\"5\":{\"NewValue\":\"石灰石\"},\"43\":{},\"47\":{},\"3\":{\"NewValue\":\"鲁254\"},\"4\":{\"NewValue\":\"1\"},\"57\":{},\"461\":{},\"175\":{},\"61\":{\"NewValue\":\" \"},\"59\":{\"NewValue\":\"000.004\"},\"456\":{},\"275\":{},\"176\":{},\"39\":{\"NewValue\":0},\"9\":{},\"10\":{\"NewValue\":0},\"327\":{}},\"Status\":\"New\"}");
        XModeRtn ValueRtn = ser.Code(Value);
       // String ValueRtn3 = ser.Code3(Value);
        Map maps=JsonToMap(_gson,ValueRtn);
        return maps;
    }

    private BillSaver getBillSaver(Map<String, Object> params)
    {
        BillTemp _BillTemp=getBillTemp(params);
        BillAdpater _Adapter =new BillAdpater(_BillTemp);
        FieldTemp temp=new FieldTemp();
        temp.setNeedSave(true);

       _Adapter.Fields.get("FOrganizeID").setValue(params.get("organizeId"));//组织id
       _Adapter.Fields.get("FCancel").setValue(0);//0
        _Adapter.Fields.get("FBillNo").setValue(getBillNo(params));//有
        _Adapter.Fields.get("FPrintTimes").setValue(0);//0
       _Adapter.Fields.get("FCarNo").setValue(params.get("FCarNo"));//车牌号，手填
       _Adapter.Fields.get("FNote").setValue("");//备注remark
        _Adapter.Fields.get("FStep").setValue(0);//0
        _Adapter.Fields.get("FType").setValue(0);//0
        _Adapter.Fields.get("FItemID").setValue(params.get("itemId"));//物料内码
       //_Adapter.Fields.get("FItemErpID").setValue(_BillSaver.getItemID());
       _Adapter.Fields.get("FItemNumber").setValue(params.get("itemNumber"));//編號
        _Adapter.Fields.get("FItemName").setValue(params.get("itemName"));//名称/
       _Adapter.Fields.get("FCustomID").setValue(params.get("FCustomID"));//客户id
        _Adapter.Fields.get("FCustomName").setValue(params.get("FCustomName"));//客户名称
       //_Adapter.Fields.get("FCustomName").setValue(_DataItem.getNote());//��̨
        _Adapter.Fields.get("FQtyPln").setValue(params.get("preload"));//预装量
        _Adapter.Fields.get("FQtyInsHistory").setValue(0);//历史皮重，填0
        _Adapter.Fields.get("FEmpRht").setValue("");//货主名，0006的真实姓名todo
       _Adapter.Fields.get("FLine").setValue(0);//0
        _Adapter.Fields.get("FBatch").setValue("");//  ""
     //_Adapter.Fields.get("FErpID").setValue(_BillSaver.getErpID());
       _Adapter.Fields.get("_FFleetID").setValue("");//  ""
       _Adapter.Fields.get("_FFleetName").setValue("");//  ""
        _Adapter.Fields.get("_FFleetNumber").setValue("");//  ""

        List<SalesOrderEntity> _ListSalesOrderEntity=getListSalesOrderEntity(params);//todo
        for(SalesOrderEntity Value: _ListSalesOrderEntity){
            int index=_Adapter.Entrys.get(1).AddRow();
            if(index==1){
                _Adapter.Fields.get("FOrderNo").setValue(Value.getOrderNo());
                //_Adapter.Fields.get("FErpID").setValue(Value.getOrderNo());
                _Adapter.Fields.get("FErpNo").setValue(Value.getOrderNo());

            }

            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FOrderID").setValue(Value.getOrderID());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FOrderAssID").setValue(Value.getOrderAssID());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FOrderNumber").setValue(Value.getOrderNo());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FCustID").setValue(Value.getCustom().getID());
            //_Adapter.Entrys.get(1).Rows.get(index).Fields.get("FCustName").setValue(_DataItem.getNote());//��̨
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FCustName").setValue(Value.getCustom().getName());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FCustNumber").setValue(Value.getCustom().getNumber());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FPrice").setValue(Value.getPrice());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FBalance").setValue(Value.getBalance());
            _Adapter.Entrys.get(1).Rows.get(index).Fields.get("FAllot").setValue(Value.getDeduct());

        }

        return _Adapter.GetSaveItem();





        }

    private String getBillNo(Map<String, Object> params) {
        Map map=getNumber(params);
        String b= (String) map.get("Data");
        String[] b2=b.split("billno=");
        return  b2[1].substring(0,6);
        //return  "0000125";

    }

    private List<SalesOrderEntity> getListSalesOrderEntity(Map<String, Object> params) {
        List<SalesOrderEntity> _ListSalesOrderEntity=new ArrayList<>();
        SalesOrderEntity Value=new SalesOrderEntity();
        Value.setOrderID((String) params.get("OrderId"));
        Value.setAssID((String) params.get("AssID"));
        Value.getCustom().setID((String) params.get("customerid"));
        Value.setOrderNo((String) params.get("orderNumber"));
        Value.getCustom().setName((String) params.get("FCustomName"));
        Value.getCustom().setNumber((String) params.get("CustomNo"));
        Value.setPrice(Double.parseDouble((String) params.get("Price")));
        Value.setBalance((Integer) params.get("balance"));
        Value.setDeduct(Double.parseDouble(params.get("preload").toString()));//todo
        _ListSalesOrderEntity.add(Value);
        return _ListSalesOrderEntity;
    }



    private Map JsonToMap(Gson _gson,XModeRtn ValueRtn)
    {
        Map<String, Object> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<Map<String, Object>>() {
        }.getType());
        for (Map.Entry<String, Object> entry : maps.entrySet()) {
            System.out.println("key: " + entry.getKey() + "  " + "value: " + entry.getValue());

        }
        return maps;
    }
    private List JsonToList(Gson _gson, XModeRtn ValueRtn)
    {
        List<String> maps = _gson.fromJson(ValueRtn.getJsonObject(), new TypeToken<List<Object>>() {
        }.getType());
        return maps;
    }

}
